package com.mrx.commons.util.redis;

import com.mrx.commons.util.redis.adapter.FakeRedisTemplateAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.lang.NonNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Mr.X
 * @since 2022-09-18 12:26
 */
public class FakeRedisTemplate<K, V> extends FakeRedisTemplateAdapter<K, V> {

    private static final Map<Object, ValueWrapper> fakeRedis = new ConcurrentHashMap<>();

    private static final Logger logger = LoggerFactory.getLogger(FakeRedisTemplate.class);

    private final ValueOperations<K, V> valueOps = new FakeValueOperations<>(fakeRedis);

    public FakeRedisTemplate() {
        logger.info("构造 fakeRedis");
    }

    @Override
    public Boolean delete(@NonNull K key) {
        boolean res = fakeRedis.containsKey(key);
        fakeRedis.remove(key);
        logger.debug("移除 key: {}", key);
        return res;
    }

    @NonNull
    @Override
    public ValueOperations<K, V> opsForValue() {
        return valueOps;
    }

}
